<!doctype html>
<html>
<head>
  <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
  <title>test-results-flakiness</title>

  <script src="../bower_components/webcomponentsjs/webcomponents-lite.js"></script>
  <script src="../bower_components/web-component-tester/browser.js"></script>

  <!-- Import the element to test -->
  <link rel="import" href="../src/test-results-flakiness.html">
</head>
<body>

  <test-fixture id="test-results-flakiness-fixture">
    <template>
      <test-results-flakiness></test-results-flakiness>
    </template>
  </test-fixture>

  <script>
    (function() {
      'use strict';
      suite('<test-results-flakiness>', function() {
        let fix;

        setup(function() {
          fix = fixture('test-results-flakiness-fixture');

          stub('iron-ajax', {
            generateRequest: function() {
              if (this.url == '/test_flakiness/groups') {
                this._setLastResponse([
                  {name: 'foo/bar/zee', kind: 'dir'},
                  {name: 'DOM', kind: 'team'},
                  {name: 'unknown-team', kind: 'unknown-team'},
                  {name: 'unknown-dir', kind: 'unknown-dir'},
                  {name: 'FooBar', kind: 'test-suite'},
                ]);
              } else if (this.url == '/test_flakiness/list') {
                this._setLastResponse([
                  {
                    test_name: 'foo/bar/zee/test.html',
                    flakiness: 0.01345,
                    normalized_step_name: 'test_unittests',
                    total_flaky_failures: 10,
                    total_tries: 2000,
                  }
                ]);
              }
            }
          });

        });

        test('loads suggestions for omnibox', function(done) {
          fix.route = {path: '/'};
          assert.equal(fix._group.kind, 'all');
          assert.equal(fix._group.name, 'all');
          assert.equal(fix.$.omnibox.value, '');
          assert.deepEqual(
              fix.$.omnibox.items,
              [
                {label: 'dir:foo/bar/zee', value: 1},
                {label: 'dir:unknown', value: 4},
                {label: 'team:DOM', value: 2},
                {label: 'team:unknown', value: 3},
                {label: 'test-suite:FooBar', value: 5},
              ]
          );
          done();
        });

        test('loads data for selected group', function(done) {
          fix.route = {path: '/dir/foo/bar/zee'};
          flush(function() {
            // Correctly parses selected group.
            assert.equal(fix._group.kind, 'dir');
            assert.equal(fix._group.name, 'foo/bar/zee');

            // Updates selected value in omnibox.
            assert.equal(fix.$.omnibox.value, 1);

            // Populates tests table with correct data and links.
            assert.equal(fix.$.tests.querySelectorAll('.grid-row').length, 1)
            assert.equal(
                fix.$.tests.querySelector('.grid-row-link').href.substr(-94),
                '/dashboards/flakiness_dashboard.html#' +
                'testType=test_unittests&tests=foo%2Fbar%2Fzee%2Ftest.html')
            assert.equal(
                fix.$.tests.querySelector('.grid-row .test-name').innerText,
                'foo/bar/zee/test.html')
            let stats = fix.$.tests.querySelectorAll('.grid-row .test-stats');
            assert.equal(stats[0].innerText, '10 / 2000')
            assert.equal(stats[1].innerText, '1.34%')
            done();
          });
        });

        test('error is formatted to be user-readable', function(done) {
          let oldConsoleError = console.error;
          let lastConsoleErrorArgs;
          console.error = function() { lastConsoleErrorArgs = arguments; }

          lastConsoleErrorArgs = undefined;
          assert.equal(fix._formatError({}), 'Unknown error');
          assert.deepEqual(lastConsoleErrorArgs[0], {})

          lastConsoleErrorArgs = undefined;
          assert.equal(
              fix._formatError({'error': {'a': 'b'}}), 'Unknown error');
          assert.deepEqual(lastConsoleErrorArgs[0], {'error': {'a': 'b'}})

          lastConsoleErrorArgs = undefined;
          assert.equal(fix._formatError({'error': 'foobar'}), 'foobar');
          assert.isUndefined(lastConsoleErrorArgs);

          console.error = oldConsoleError;
          done();
        });
      });
    })();
  </script>

</body>
</html>
